//
//  YWCreateTempMissionTableViewController.m
//  maintain
//
//  Created by 烟雨云渊 on 16/8/16.
//  Copyright © 2016年 ewin. All rights reserved.
//

#import "YWCreateTempMissionTableViewController.h"
#import "UIColor+CustomColors.h"
#import "YWMacro.h"
#import "YWCommonTool.h"

#import "YWLoginUser.h"
#import "YWLoginTool.h"

#import "YWEquipmentManager.h"
#import "YWBuildingManager.h"
#import "YWBuilding.h"
#import "YWInspectionLineRecord.h"
#import "YWPatrollineRecord.h"
#import "YWMission.h"
#import "YWEquipment.h"
#import "YWInspectionMissionModel.h"

//#import "YWDoTempEquipmentInspectionTableViewController.h"
#import "YWDoTempEquipmentMissionTableViewController.h"
#import "YWDoLocationInspectionTableViewController.h"
#import "YWDoKeepWatchMissionTableViewController.h"
//#import "YWDoTempDeteAndUpKeepMissionTableViewController.h"
#import "YWDoTempDeteAndUpKeepEquTypeMissionTableViewController.h"
#import "YWDoTempTroubleMissionTableViewController.h"
#import "YWTroubleMissionModel.h"
#import "YWReportTroubleModel.h"
#import "YWLocation.h"

@interface YWCreateTempMissionTableViewController ()
@property (nonatomic, strong) UILabel *footLabel;
@end

@implementation YWCreateTempMissionTableViewController
- (instancetype)init{
    return [self initWithStyle:UITableViewStyleGrouped];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    NSString *type = [YWCommonTool maintenanceTypeOfMaintenanceTypeId:self.maintenanceType];
    if (self.maintenanceType == YWMaintenanceTypePatrol) {
        if (self.isLocationInspection == YES) {
            type = @"位置巡查";
        }else if (self.isEquipmentInspection == YES){
            type = @"设备巡查";
        }
    }
    self.title = type;
    self.tableView.backgroundColor = [UIColor customBackgroudColor];
    self.tableView.separatorColor = [UIColor customLineColor];
    [self.tableView addSubview:self.footLabel];
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:YES];
    [self.allEquArray enumerateObjectsUsingBlock:^(YWEquipment*  _Nonnull equipment, NSUInteger idx, BOOL * _Nonnull stop) {
        equipment.totalImageNameArray = nil;
        equipment.psText = nil;
//        equipment.runTimeParameterArray = nil;
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (self.maintenanceType == YWMaintenanceTypePatrol) {
        return (self.isEquipmentInspection == NO && self.isLocationInspection == NO) ? 2 : 1;
    }else{
        return 1;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIdentifier = @"YWCreateTempMissionTableVCCellIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    if (self.maintenanceType == YWMaintenanceTypePatrol && self.isEquipmentInspection == NO && self.isLocationInspection == NO) {
        if (indexPath.row==0) {
            cell.textLabel.text = @"执行临时位置巡查";
        }else{
            cell.textLabel.text = @"执行临时设备巡查";
        }
    }else{
        cell.textLabel.text = [NSString stringWithFormat:@"执行临时%@",self.title];
    }
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 54;
}

#pragma mark - Table view section headView
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    static NSString* HeaderIdentifier = @"YWCreateTempMissionTableViewControllerForHeaderInSection";
    UITableViewHeaderFooterView* headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:HeaderIdentifier];
    if (headerView == nil) {
        headerView = [[UITableViewHeaderFooterView alloc] initWithReuseIdentifier:HeaderIdentifier];
        UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake(30, 20, SCREEN_WIDTH-30*2, 50)];
        [headerView addSubview:label];
        label.textAlignment = NSTextAlignmentCenter;
        label.tag = 2006;
        label.numberOfLines = 2;
        label.font = kContentFont;
        label.textColor = [UIColor lightGrayColor];
    }
    headerView.contentView.backgroundColor = [UIColor customBackgroudColor];
    NSString *headText = @"此二维码暂无待执行任务，您可选择执行临时任务";
    UILabel* label = [headerView viewWithTag:2006];
    label.text = headText;
    return headerView;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 90;
}
-(UILabel *)footLabel{
    if (!_footLabel) {
        UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake(30, self.tableView.frame.size.height-64-80, SCREEN_WIDTH-30*2, 80)];
        label.textAlignment = NSTextAlignmentCenter;
        label.numberOfLines = 2;
        label.backgroundColor = [UIColor customBackgroudColor];
        label.font = kContentFont;
        label.textColor = [UIColor lightGrayColor];
        label.text = @"易维保建议您先制定计划并下发任务，以便您进行监管及统计";
        _footLabel = label;
    }
    return _footLabel;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (self.maintenanceType == YWMaintenanceTypePatrol) {
        if (self.isEquipmentInspection == NO && self.isLocationInspection == NO) {
            if (indexPath.row==0) {
                [self doTempLocationInspection];
            }else{
                [self doTempEquipmentInspection];
            }
        }else{
            if (self.isLocationInspection) {
                [self doTempLocationInspection];
            }else if (self.isEquipmentInspection){
                [self doTempEquipmentInspection];
            }
        }
    }else if (self.maintenanceType == YWMaintenanceTypeKeepWatch){
        [self doTempKeepWatchMission];
    }else if (self.maintenanceType == YWMaintenanceTypeDetection || self.maintenanceType == YWMaintenanceTypeUpkeep){
        [self doTempDetectAndKeepUpMission];
    }else if (self.maintenanceType == YWMaintenanceTypeTrouble){
        [self doTempTroubleMission];
    }
}

- (void)doTempLocationInspection{
    //临时位置巡查
    YWInspectionLineRecord *record = [[YWInspectionLineRecord alloc]init];
    //    record.inspectionLineId = inspectionMission.inspectionLine.inspectionLineId;
    NSString *uniqueTag = [NSString stringWithFormat:@"temp_%lld",(long long)[YWCommonTool negativeTimestampId]/1000];;
    record.uniqueTag = uniqueTag;
    record.count = 0;
    NSTimeInterval nowTime = [[NSDate date] timeIntervalSince1970]*1000;
    record.startTime = nowTime;
    record.endTime = nowTime;
    record.result = YWInspectionLineRecordResultUnfinishAndNormal;
    YWLoginUser *currentUser = [YWLoginTool loginUser];
    record.executorIds = [NSString stringWithFormat:@"%ld",(long)currentUser.uniqueId];
    //    record.inspectionMissionId = inspectionMission.inspectionMissionId;
    
    YWDoLocationInspectionTableViewController *doTempLocationInspectionVC = [[YWDoLocationInspectionTableViewController alloc] init];

    NSInteger locationId = [[YWEquipmentManager shareManager]locationIdWithQrcodeId:self.qrcodeId];
    doTempLocationInspectionVC.locationId = locationId?:self.locationId;
    doTempLocationInspectionVC.tempInspectionLineRecord = record;
    doTempLocationInspectionVC.qrcodeId = self.qrcodeId;
    doTempLocationInspectionVC.isFromLedger = YES;
    [self.navigationController pushViewController:doTempLocationInspectionVC animated:YES];
}

- (void)doTempEquipmentInspection{
    //临时设备巡查
    YWDoTempEquipmentMissionTableViewController *doTempEquipmentInspectionVC = [[YWDoTempEquipmentMissionTableViewController alloc] init];
    doTempEquipmentInspectionVC.qrcodeID = self.qrcodeId;
    doTempEquipmentInspectionVC.locationId = self.locationId;
    doTempEquipmentInspectionVC.allEquArray = self.allEquArray;
    doTempEquipmentInspectionVC.currentEquipmentId = self.currentEquipmentId;
    doTempEquipmentInspectionVC.inspectionMission = [[YWInspectionMissionModel alloc] init];
    doTempEquipmentInspectionVC.isFromLedger = YES;
    [self.navigationController pushViewController:doTempEquipmentInspectionVC animated:YES];
}

- (void)doTempKeepWatchMission{
    //临时巡更
    YWPatrollineRecord *record = [[YWPatrollineRecord alloc]init];
    //            record.patrolLineId = 0;
    NSString *uniqueTag = [NSString stringWithFormat:@"temp_%lld",(long long)[YWCommonTool negativeTimestampId]/1000];
    record.uniqueTag = uniqueTag;
    //            record.status = YWPatrollineRecordStatusUnfinish;
    record.count = 0;
    NSTimeInterval nowTime = [[NSDate date] timeIntervalSince1970]*1000;
    record.startTime = nowTime;
    record.endTime = nowTime;
    record.result = YWPatrollineRecordResultUnfinishAndNormal;
    YWLoginUser *currentUser = [YWLoginTool loginUser];
    record.executorIds = @(currentUser.uniqueId).stringValue;
    //            record.keepWatchMissionId = 0;
    //            页面跳转
    YWDoKeepWatchMissionTableViewController *writeKeepWatchInfoVC = [YWDoKeepWatchMissionTableViewController new];
    writeKeepWatchInfoVC.tempPatrolLineRecord = record;
    NSInteger locationId = [[YWEquipmentManager shareManager]locationIdWithQrcodeId:self.qrcodeId];
    writeKeepWatchInfoVC.locationId = locationId?:self.locationId;
    writeKeepWatchInfoVC.qrcodeId = self.qrcodeId;
    writeKeepWatchInfoVC.isFromLedger = YES;
    [self.navigationController pushViewController:writeKeepWatchInfoVC animated:YES];
}

- (void)doTempDetectAndKeepUpMission{
    //临时检测,保养
    YWDoTempDeteAndUpKeepEquTypeMissionTableViewController *missionVC = [[YWDoTempDeteAndUpKeepEquTypeMissionTableViewController alloc]init];
    missionVC.qrCodeId = self.qrcodeId;
    missionVC.locationId = self.locationId;
    missionVC.allEquArray = self.allEquArray;
//    missionVC.currentEquipmentId = self.currentEquipmentId;
    missionVC.maintenanceType = self.maintenanceType;
    [self.navigationController pushViewController:missionVC animated:YES];
}

- (void)doTempTroubleMission{
    //临时维修
    YWDoTempTroubleMissionTableViewController *troubleVC = [[YWDoTempTroubleMissionTableViewController alloc]init];
    troubleVC.allEquArray = self.allEquArray;
    troubleVC.troubleMissionModel = [self createTempTroubelMissionModel];
    [self.navigationController pushViewController:troubleVC animated:YES];
}

//- (YWTroubleMission*)createTempTroubelMission{
//    YWTroubleMission *troubleMission = [[YWTroubleMission alloc] init];
//    troubleMission.troubleId = -1 * [YWCommonTool randNumber];
//    troubleMission.qrcodeId = self.qrcodeId;
//    troubleMission.locationId = self.equipment.locationId;
//    troubleMission.equipmentId = self.equipment.equipmentId;
//    
//    troubleMission.maintenanceTypeId = YWMaintenanceTypeTrouble;
//    troubleMission.troubleStatus = YWTroubleMissionStatusFinish;
//    troubleMission.isPostTrouble = NO;
//    
//    NSInteger locationId = [[YWEquipmentManager shareManager]locationIdWithQrcodeId:self.qrcodeId];
//    YWBuilding* building = [[YWBuildingManager shareManager] buildingWithLocationId:locationId];
//    troubleMission.buildingId = building.buildingId;
//    return troubleMission;
//}

- (YWTroubleMissionModel*)createTempTroubelMissionModel{
    YWTroubleMissionModel * troubleMissionModel = [[YWTroubleMissionModel alloc] init];
    YWReportTroubleModel * reportTrouble = [[YWReportTroubleModel alloc] init];
    reportTrouble.troubleId = -1 * [YWCommonTool randNumber];
    reportTrouble.qrcodeId = self.qrcodeId;
    YWLocation * location = [[YWBuildingManager shareManager] locationWithId:self.equipment.locationId];
    reportTrouble.location = location;
    reportTrouble.equipment = self.equipment;
    reportTrouble.maintenanceTypeId = YWMaintenanceTypeTrouble;
    reportTrouble.troubleStatus = YWTroubleMissionStatusFinish;
    
    NSInteger locationId = [[YWEquipmentManager shareManager]locationIdWithQrcodeId:self.qrcodeId];
    YWBuilding* building = [[YWBuildingManager shareManager] buildingWithLocationId:locationId];
    reportTrouble.building = building;
    troubleMissionModel.trouble = reportTrouble;
    troubleMissionModel.troubleStatus = YWTroubleMissionStatusFinish;
    return troubleMissionModel;
}
@end
